<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 日志表操作类
 */
class Logs_mdl extends CI_Model {
	
	const TABLE = 'logs';
  
	function __construct()
	{
		parent::__construct();
		
	}
	
	/**
	 * 添加日志
	 * @param $state  	状态 1 成功 2失败 3 其他
	 * @param $describe 描述 
	 */
	public function add($state,$describe)
	{
		$sess_name = $this->session->userdata('username');
		$data = array(
			'user' => empty($sess_name) ? $this->input->post('username') : $this->session->userdata('username'),
			'action' => $this->router->fetch_class().'/'.$this->router->fetch_method(),
			'create_time' => $_SERVER['REQUEST_TIME'],
			'ip' => $this->input->ip_address(),
			'state' => $state,
			'query' => $this->db->last_query(),
			'describe' => $describe
		);
		$this->db->insert(self::TABLE,$data);
	}
	
	/**
	 * 统计所有日志
	 * @param $tmp	查询条件
	 */
	 public function get_count($tmp = array()){
	 	foreach($tmp as $k => $v){
	 		$this->db->where($k,$v);
	 	}
		return $this->db->count_all_results(self::TABLE);
	 }
	 
	 /**
	  * 日志信息查询
	  * @param $limit	//每页显示多少数据
	  * @param $tmp		//where 条件
	  */
	 public function get_page($limit = 20,$tmp = array()){
	 	$p = $this->input->get('per_page',TRUE);
		foreach ($tmp as $k => $v){
			$this->db->where($k,$v);
		}
	 	$this->db->limit($limit,empty($p)?0:$p);
		$this->db->order_by('Id','DESC');
		$query = $this->db->get(self::TABLE);
		return $query->result_array();
	 }
}